//
//  SpeakTableViewCell.m
//  UI_Project_A
//
//  Created by GH on 16/2/26.
//  Copyright © 2016年 GH. All rights reserved.
//

#import "SpeakTableViewCell.h"
#import "ModelOfSpeak.h"
#import <UIImageView+WebCache.h>

#define  WIDTH [UIScreen mainScreen].bounds.size.width


@interface SpeakTableViewCell ()


@end

@implementation SpeakTableViewCell


- (void) dealloc {

    [_labelOfAnswerContect release];
    [_labelOfAnswerName release];
    [_imageOfAnswer release];
    [_labelOfContect release];
    [_labelOfAskName release];
    [_imageOfHeadPicture release];
    [_viewOftemp release];
    [_labelOfTime release];
    [super dealloc];
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
        [self createheadViewPic];
        [self createLabel];
    }


    return self;
}



-(void)passModelWith:(ModelOfSpeak *)model {

    self.labelOfAskName.text = model.userName;
    self.labelOfAskName.textColor = [UIColor grayColor];
  
    [self.imageOfHeadPicture sd_setImageWithURL:[NSURL URLWithString:model.userHeadPicUrl] placeholderImage:[UIImage imageNamed:@"touxiang"]];
    
    
    
    //contect
    self.labelOfContect.text = model.content[0];


    [self.imageOfAnswer sd_setImageWithURL:[NSURL URLWithString:model.specialistHeadPicUrl]];
    
    self.labelOfAnswerName.text = model.specialistName;
    self.labelOfAnswerContect.text = model.content[1];
    
    
    //回复时间
    //时间
    
    
    CGFloat modelTime = model.cTime.integerValue;
//    NSLog(@"%@",[modelTime class]);

    
    modelTime = modelTime / 1000;
//  NSString *modelTime =   model.cTime / 1000;
    
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm"];
    [dateFormatter setDateFormat:@"MM-dd"];
    [dateFormatter release];
    
    
    
    
    
    NSDate *now = [NSDate date];
    NSTimeInterval time = [now timeIntervalSince1970];
    float seconds = (int)time - modelTime;
    float points = seconds / 60;
    float hours = points / 60;

    
    if (points < 60) {
        self.labelOfTime.text = [NSString stringWithFormat:@"%d分钟前", (int)points];
    }else if (points > 60 && points < 3600) {
        
        self.labelOfTime.text = [NSString stringWithFormat:@"1小时前"];
        
    }else if (points > 3600 && points< 7200) {
        self.labelOfTime.text = [NSString stringWithFormat:@"2小时前"];
    }else if (points > 7200 && points< 10800) {
        self.labelOfTime.text = [NSString stringWithFormat:@"3小时前"];
    }else if (points > 10800 && points< 14400) {
        self.labelOfTime.text = [NSString stringWithFormat:@"4小时前"];
    }else if (points > 14400 && points< 18000) {
        self.labelOfTime.text = [NSString stringWithFormat:@"5小时前"];
    }else if (points > 18000 && points< 21600) {
    }else if (hours > 24 && hours < 48) {
    
        self.labelOfTime.text = [NSString stringWithFormat:@"一天前"];
    
    }else {
    
        self.labelOfTime.text = [NSString stringWithFormat:@"二天前"];

    
    }
    

    
    

}



//边框view
- (void)createView {
    self.viewOftemp = [[UIView alloc] init];
    [self.contentView addSubview:self.viewOftemp];
    self.viewOftemp.layer.masksToBounds = YES;
    self.viewOftemp.layer.cornerRadius = 5;
    self.viewOftemp.layer.borderWidth = 1;
    self.viewOftemp.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.viewOftemp.layer.shadowOffset = CGSizeMake(0, 1);
    self.viewOftemp.layer.shadowOpacity = 0.2;
    
    
}


- (void)createLabel {

    self.labelOfAskName = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelOfAskName];

    self.labelOfContect = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelOfContect];
    self.labelOfContect.numberOfLines = 0;
    self.labelOfContect.textColor = [UIColor grayColor];
    
    self.labelOfAnswerName = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelOfAnswerName];
    
    
    self.labelOfAnswerContect = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelOfAnswerContect];
    self.labelOfAnswerContect.numberOfLines = 0;
    
    
    self.labelOfTime = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelOfTime];

    

}


- (void)createheadViewPic {

    //头像
    self.imageOfHeadPicture = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
    [self.contentView addSubview:self.imageOfHeadPicture];
    [self.imageOfHeadPicture release];
    self.imageOfHeadPicture.layer.masksToBounds = YES;
    self.imageOfHeadPicture.layer.cornerRadius = 20;
   
    
    self.imageOfAnswer = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
    [self.contentView addSubview:self.imageOfAnswer];
    [self.imageOfAnswer release];
    self.imageOfAnswer.layer.masksToBounds = YES;
    self.imageOfAnswer.layer.cornerRadius = 20;
  




}

- (void)layoutSubviews {
    [super layoutSubviews];
   self.viewOftemp.frame = CGRectMake(10, 10, self.frame.size.width - 20, self.frame.size.height - 20);
//头像尺寸
    self.imageOfHeadPicture.frame = CGRectMake(22, 30, 40, 40);
    self.labelOfAskName.frame = CGRectMake(70, 35, self.frame.size.width - 80, 30);
    self.labelOfContect.frame = CGRectMake(70, 70, self.frame.size.width - 100, [[self class] heightForLabel:self.labelOfContect.text]);

    CGFloat h = [[self class] heightForLabel:self.labelOfContect.text];
    self.imageOfAnswer.frame = CGRectMake(20, h + 60 + 100, 40, 40);
    
    self.labelOfAnswerName.frame = CGRectMake(70, h + 60 + 100, 70, 30);
    self.labelOfAnswerContect.frame = CGRectMake(70, h+ 60 + 100 + 30, self.frame.size.width - 100, [[self class] heightForLabel:self.labelOfAnswerContect.text]);
    
    CGFloat z = h + 190 + [[self class] heightForLabel:self.labelOfAnswerContect.text];
    self.labelOfTime.frame = CGRectMake(self.frame.size.width - 90, z + 10, 80, 40);
    self.labelOfTime.font = [UIFont systemFontOfSize:14];
    self.labelOfTime.textColor = [UIColor grayColor];
    
    self.g = z + 40 +20;


}


+(CGFloat)heightForLabel:(NSString *)text{
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake(WIDTH - 100, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
    
    
    return rect.size.height;
}

+ (CGFloat)heightForCell:(ModelOfSpeak *)model {
    
    CGFloat h = [SpeakTableViewCell heightForLabel:model.content[0]] + [SpeakTableViewCell heightForLabel:model.content[1]]+ 190 + 60;

    
    
    
    
    
    return h;

}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
